package com.zhkj.txg.module.product.ui

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.lifecycle.Observer
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.ScreenUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.product.adapter.ShareProductUIEntity
import com.zhkj.txg.module.product.vm.ShareViewModel
import com.zhkj.txg.utils.ShareUtils
import com.zhkj.txg.utils.ViewPagerTransformer
import kotlinx.android.synthetic.main.activity_product_share.*

class ProductShareActivity : ViewModelActivity<ShareViewModel>() {

    companion object {
        private val LIST = "list"
        private val NAME = "name"
        private val PRICE = "price"
        private val GOODS_ID = "goodsId"
        fun open(activity: Activity, goodsId: Long, images: Array<String>, productName: String, productPrice: String) {
            val intent = Intent(activity, ProductShareActivity::class.java).putExtra(GOODS_ID, goodsId)
                .putExtra(LIST, images).putExtra(NAME, productName).putExtra(PRICE, productPrice)
            activity.startActivity(intent)
        }
    }

    private val fragment: MutableList<ProductShareFragment> = mutableListOf()

    override fun setViewModel(): ShareViewModel = createViewModel()
    override val layoutResId: Int = R.layout.activity_product_share
    override fun statusTitleBar(): View? = TitleShare

    override fun initUI() {
        with(vpShare) {
            pageMargin = ScreenUtil.dip(context, 20)
            setPageTransformer(false, ViewPagerTransformer())
        }
        // 保存单张图片
        tvShareSaveSingle.setOnClickListener {
            requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, {
                if (fragment.isNotEmpty()) viewModel.saveView2Picture(fragment[vpShare.currentItem].root())
            }, { toast(getString(R.string.read_permission_deny)) })
        }
        // 保存全部图片
        tvShareSaveAll.setOnClickListener {
            requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, {
                val list = mutableListOf<View>()
                if (fragment.isNotEmpty()) {
                    fragment.forEach { list.add(it.root()) }
                    viewModel.saveView2Picture(list)
                }
            }, { toast(getString(R.string.read_permission_deny)) })
        }
        // 分享到微信
        tvShareWeChat.setOnClickListener {
            requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, {
                if (fragment.isNotEmpty()) viewModel.saveView2Picture(fragment[vpShare.currentItem].root(), true)
            }, { toast(getString(R.string.read_permission_deny)) })
        }
    }

    override fun initViewModel() {
        // 获取二维码数据观察者
        viewModel.qrCodeLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.data?.let { code ->
                val list = intent.getStringArrayExtra(LIST)?.asList()
                val productName = intent.getStringExtra(NAME) ?: ""
                val productPrice = intent.getStringExtra(PRICE) ?: ""
                val introduce = AccountManager.getNickname()
                list?.forEach {
                    val data = ShareProductUIEntity(productName, productPrice, introduce, code.qrCode, it)
                    fragment.add(ProductShareFragment.newInstance(data))
                }
                // 配置ViewPager
                vpShare.offscreenPageLimit = fragment.size
                vpShare.adapter =
                    object : FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
                        override fun getItem(position: Int): Fragment = fragment[position]
                        override fun getCount(): Int = fragment.size
                    }
            }
        })
        // 保存图片观察者
        viewModel.saveViewLiveData.observe(this, Observer {
            if (it.share2WeChat) ShareUtils.shareImage2Friends(this, it.filePath)
            else toast(getString(R.string.save_picture_success))
        })
    }

    override fun initData() {
        viewModel.getQRCode(intent.getLongExtra(GOODS_ID, 0))
    }
}